package com.example.person_web.core.controller;

import com.example.person_web.common.WrapperResponse;
import com.example.person_web.core.component.CustomUserDetailsService;
import com.example.person_web.core.component.JwtUtil;
import com.example.person_web.core.dto.UserInfoDTO;
import com.example.person_web.core.entity.UserInfoDO;
import com.example.person_web.core.service.LoginRegisterService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/core/web/auth")
public class LoginRegisterController {
    @Autowired
    private LoginRegisterService loginRegisterService;
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 登录接口
     *
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/sinin")
    public WrapperResponse login(@RequestBody UserInfoDTO userInfoDTO) throws Exception {

        WrapperResponse<UserInfoDO> wrapperResponse = loginRegisterService.login(userInfoDTO);
        if(wrapperResponse.getCode() == 0){
            // 加载用户详细信息
            final UserDetails userDetails = userDetailsService.loadUserByUsername(userInfoDTO.getAccount());
            // 生成JWT
            final String jwt = jwtUtil.generateToken(userDetails);
            wrapperResponse.getData().setJwt(jwt);
        }
        return wrapperResponse;

    }

    /**
     * 注册接口
     *
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/register")
    public WrapperResponse register(@RequestBody UserInfoDTO userInfoDTO) {
        return loginRegisterService.register(userInfoDTO);
    }

    /**
     * 获取验证码
     *
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/queryCode")
    public WrapperResponse queryCode(@RequestBody UserInfoDTO userInfoDTO) throws IOException {
        return loginRegisterService.queryCode(userInfoDTO);
    }

    /**
     * 自动登录接口
     *
     * @return
     */
    @GetMapping("autoLogIn")
    public WrapperResponse autoLogIn(HttpServletRequest request){
        WrapperResponse<UserInfoDO> wrapperResponse = loginRegisterService.autoLogIn(request);
        if(wrapperResponse.getCode() == 0){
            // 加载用户详细信息
            final UserDetails userDetails = userDetailsService.loadUserByUsername(wrapperResponse.getData().getAccount());
            // 生成JWT
            final String jwt = jwtUtil.generateToken(userDetails);
            wrapperResponse.getData().setJwt(jwt);
        }
        return wrapperResponse;
    }
    @PostMapping("setBackground")
    public WrapperResponse setBackground(@RequestBody String imageUrl,HttpServletRequest request){
        return loginRegisterService.setBackground(imageUrl,request);
    }
}
